package com.cqc.fragmentreplace.activity;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.cqc.fragmentreplace.fragment.Frag1;
import com.cqc.fragmentreplace.fragment.Frag2;
import com.cqc.fragmentreplace.fragment.Frag3;
import com.cqc.fragmentreplace.fragment.Frag4;
import com.cqc.fragmentreplace.R;

public class ReplaceActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Frag1 frag1;
    private Frag2 frag2;
    private Frag3 frag3;
    private Frag4 frag4;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_replace);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

        initFragment();
    }

    private void initFragment() {
        frag1 = new Frag1();
        frag2 = new Frag2();
        frag3 = new Frag3();
        frag4 = new Frag4();
    }

    @Override
    public void onClick(View view) {
        transaction = getSupportFragmentManager().beginTransaction();
        switch (view.getId()) {
            case R.id.btn1:
                transaction.replace(R.id.frame, frag1, "Frag1").commit();
                break;
            case R.id.btn2:
                transaction.replace(R.id.frame, frag2, "Frag2").commit();
                break;
            case R.id.btn3:
                transaction.replace(R.id.frame, frag3, "Frag3").commit();
                break;
            case R.id.btn4:
                transaction.replace(R.id.frame, frag4, "Frag4").commit();
                break;

        }
    }
}
